<?php
class Subcategory extends CI_Controller 
{
		
    function __construct()
	{
		parent::__construct();	
		$this->load->model('category_model');
		$this->load->language('subject');
	}
	function getPath($action = '')
	{
		return array('controller' => strtolower(__CLASS__), 'action' => strtolower($action));
	}
	function index($page = 0)
	{
	    $view                    = $this->getPath(__FUNCTION__);
		$where                   = array('deleted' => 0,'cat_type' => 'c');
		$where1                  = array('deleted' => 0,'cat_type' => 'p'); 
		$search                  = $this->input->post('search');
		if(!empty($search))
		{
			$category             = $this->input->post('category');             
			$view['filter1']      = $category ;  
			
			$this->session->set_userdata(array('category_filter' => $category));         // set filter value
			
		}
		else
		{
			$view['filter1']    = $this->session->userdata('category_filter');
		}
		if(!empty($view['filter1']))
		{
			
		$where['parent_id']     = $view['filter1'];   //$course;
		}
		//echo $view['filter1'];
		$per_page_count         = $this->config->item('per_page_count');
		$category               = $this->category_model->getcategories($page, $per_page_count,'',$where1);
		
		$view['category'] 		= $category->records;
		$view['cur_page'] 	    = $page;
		$view['title']		    = 'Subject Listing';
		$view['subtitle']	    = 'Subject Lists';
		/*********************** Pagination block **************************************/
		$config['base_url'] 	= $this->config->item('admin_base_url_url').'/subject/index/';
		$config['total_rows'] 	= $subject->total;
		$config['per_page']     = $per_page_count;
		$config['cur_page']     = $page /$per_page_count;
		$config['uri_segment'] 	= 4;
		$config['num_links'] 	= 3;
		$this->pagination->initialize($config);
		$view['msg'] 	        = $this->lang->line($this->session->flashdata('subject_msg'));
		$categories             = $this->category_model->getCategories($page, $per_page_count, $like, $where);
		$view['categories'] 		  = $categories->records;
		$this->layout->adminview('category/subcategory_view', $view);
	
	}
	function add($cur_page = 0)
	{
		$view                    = $this->getPath(__FUNCTION__);
		/************************** check permission *********************************************/
	
		/************************** check permission *********************************************/
		$view['title']          = "SubCategory Details";
		$save                   = $this->input->post('save');
		$view['subject'] 	    = array();
		$view['cur_page']       = $cur_page;
		$view['subtitle']	    = 'Add Subcategory';
		$where                  = array('deleted' => 0,'cat_type' => 'p');
		if(!empty($save)) 
		{
			$data               = $this->retrieve->post($this->category_model->fields);
			$data['cat_type']     = "c";
			$insertStatus = $this->category_model->add($data);
			$view['category'] 	= $data;
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('category_msg', 'category_add_success');
					if(!empty($cur_page)) 
						redirect($this->config->item('admin_base_url').'subcategory/index/'.$cur_page,'location');
					else
						redirect($this->config->item('admin_base_url').'subcategory/index/','location');
					exit;		
					break;
				case -1:
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('category_valid_failed');
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('category_already_exists');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('category_add_failure');
	
					break;
				
				default:
					$view['msg'] 	= $this->lang->line('categoryt_add_failure');
					break;
			}
		} 

		$categories             = $this->category_model->getCategories(0, '', '', $where);
		$view['categories'] 	   = $categories->records;
		$this->layout->adminview('category/subcategory_addedit', $view);
	}

	function edit($id = '', $cur_page='')
	{
		$view                    = $this->getPath(__FUNCTION__);
		/************************** check permission *********************************************/
		$val					 = $this->permission($view['controller'],$view['action']);
	  //  echo $val;
		if($val == 0)
		{
			redirect($this->config->item('admin_base_url').'permission/no_access/'.$cur_page,'location');
		}
		/************************** check permission *********************************************/
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url').'subcategory/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$records                    = $this->category_model->getCategories(0,1, array('id' => $id));
			$category                   = $records->records[0];
			if(empty($category)) 
			{
				$this->session->set_flashdata('category_msg', 'category_invalid_record');
				redirect($this->config->item('admin_base_url').'subcategory/index','location');
				exit;
			}
			$view['id']		            = $id;
			
			$view['category']	        = $category;
		}
		
		$view['title']                  = "Edit Category ";
		$view['page_title']             = "Edit Category";
		$view['subtitle']	            = 'Edit Category';
		$save                           = $this->input->post('save');
		if(!empty($save)) 
		{
			$data                       = $this->retrieve->post($this->category_model->fields);
			$insertStatus               = $this->category_model->udpate($id, $data);
			switch($insertStatus)
			{
				case 1:
					$this->session->set_flashdata('category_msg', 'categpory_edit_success');
					redirect($this->config->item('admin_base_url').'subcategory/index/'.$cur_page,'location');
					exit;		
					break;
				case -1:
					break;
				case -3:
					$view['msg'] 	= $this->lang->line('category_already_exists');
					break;
				case -2:
					$view['msg'] 	= $this->lang->line('category_valid_failed');
					break;
				case false:
					$view['msg'] 	= $this->lang->line('category_edit_failure');
					break;
				;
				default:
					$view['msg'] 	= $this->lang->line('category_edit_failure');
					break;
			}

			$view['subject'] 	    = $data;
		}
		$view['cur_page']           = $cur_page; 
		$this->load->model('category_model');
	
		$where1                  = array('deleted' => 0,'parent_id' => 0); 
		$categories              = $this->category_model->getCategories(0, '', '', $where1);
		$view['categories'] 	 = $categories->records;
		$category                = $this->category_model->getCategories(0, '', '', array('id'=>$id));
		$view['category'] 	     = $category->records[0];
		$this->layout->adminview('category/subcategory_addedit', $view);
	}
	
	function cancel()
	{
    redirect($this->config->item('admin_base_url').'subcategory/index','location');
		exit;	
	}
	
	function delete($id = '', $cur_page)
	{
		$view                    = $this->getPath(__FUNCTION__);
		/************************** check permission *********************************************/
		$val=$this->permission($view['controller'],$view['action']);
	  //  echo $val;
		if($val ==0)
		{
			redirect($this->config->item('admin_base_url').'permission/no_access/'.$cur_page,'location');
		}
		/************************** check permission *********************************************/
		if(empty($id)) 
		{
			redirect($this->config->item('admin_base_url_url').'subcategory/index/'.$cur_page,'location');
			exit;
		} 
		else 
		{
			$deleteStatus = $this->category_model->delete($id);
			switch($deleteStatus)
			{
				case 1:
					$this->session->set_flashdata('users_msg', 'subcategory_delete_success');
					redirect($this->config->item('admin_base_url').'subcategory/index/'.$cur_page,'location');
					exit;		
					break;
				case false:
					$view['msg'] 	= $this->lang->line('category_delete_failure');
					break;
			}
		}
		
	}
	function subjectlist($co)
	{
		
		$records            = $this->subject_model->getSubjects( 0,'',array('course_id' => $co,'subjects.deleted' => 0));
	
		$sec            = $records->records;
		echo "<option> -- Select Subject -- </option>";
		foreach($sec as $record)
				{
					echo "<option  value='".$record['subject_id']."'>".$record['subject']." </option>";
				}
	}
	
	function clearFilter()
	{
		$this->session->unset_userdata('category_filter');
		redirect($this->config->item('admin_base_url').'subcategory/index/','location');
	}
	function permission($con,$action)
	{
		
	$mylogin = $this->session->userdata('mylogin');//print_r($mylogin);
	if($mylogin['name']=='Administrator')
	return 1;
	$mylogin['id'];
	$permissions  = $this->permission_model->getPermissions(0,'','',array('action'=>$con,'method'=>$action));
	$permissions  = $permissions->records[0];
	 
	
	$groups  = $this->group_permission_model->getGrouppermissions(0,'','',array('permission_id'=>$permissions['id'],'flag'=>1));
	$groups  = $groups->records;
	//print_r($groups);
	$flag=0;
	foreach($groups as $g)
	{
	$user  = $this->admin_group_model->getAdminGroups(0,'','',array('user_id'=>$mylogin['id'],'group_id'=>$g['group_id'],'flag'=>1));
	$user  = $user->records[0];
	if(!empty($user))
	$flag=1;
	}
	
	return $flag;
		
	}
}
?>